package Jets.display
{
   import flash.display.DisplayObject;
   import flash.display.DisplayObjectContainer;
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.MouseEvent;
   
   import Jets.tick.JTick;
   import Jets.ui.skins.Skin;
   
   public class JSprite extends Sprite
   {
	   public static const DRAW:String = "draw";
	   
	   //protected var mStartPosY:Number;
	   //protected var mMaxBubbleDist:int = 66;
	   //protected var mWhereFade:Number = 50;
	   //protected var mBubbleSpeed:Number = 0.2;
	   //protected var mBubbleDamageTimer:Timer;
	   protected var _width:Number = 50;
	   protected var _height:Number = 50;
	   protected var _skin:String;
	   protected var _state:int = 0;
	   protected var hasState:Boolean = false;
	   protected var _enabled:Boolean = true;
	   
	   protected var ms_nTickEnable:Boolean = false;
	   protected var ms_nTicker:JTick = null;
	   
	   //public var locked:Boolean = false;
	   public var drawed:Boolean = false;
	   
	   
      /**
	  * 基类
	  * @param parent显示父对象
	  * @param cx 坐标(默认0,0)
	  * @param cy 坐标
	  */ 
      public function JSprite(parent:DisplayObjectContainer=null, cx:Number=0, cy:Number=0) 
	  {
         super();
		 this.cacheAsBitmap = true;
         this.move(cx,cy);
         this.tabEnabled = false;
		 
         if(parent != null)
         {
			 parent.addChild(this);
         }
         this.init();
      }
      
	  /**注册change事件*/
	  private var __onchange:Function;
      public function get _onchange() : Function 
	  {
         return this.__onchange;
      }
      public function set _onchange(callback:Function) : void 
	  {
         if(this.__onchange != null)
         {
            removeEventListener(Event.CHANGE,this.__onchange);
         }
         this.__onchange = callback;
         if(this.__onchange != null)
         {
            addEventListener(Event.CHANGE,this.__onchange);
         }
      }
      
	  /**注册click事件*/
      private var _onpress:Function;
      public function get click() : Function 
	  {
         return this._onpress;
      }
      public function set click(callback:Function) : void 
	  {
         if(this._onpress != null)
         {
            removeEventListener(MouseEvent.CLICK,this._onpress);
         }
         this._onpress = callback;
         if(null != this._onpress)
         {
            addEventListener(MouseEvent.CLICK,this._onpress);
         }
      }
      
	  public function set TickEnable(bool:Boolean):void
	  {
		  if(ms_nTickEnable)
		  {
			  if(ms_nTickEnable != bool)
			  {
			  	ms_nTicker.stop();
				return;
			  }
			  return;
		  }
		  else
		  {
			  if(ms_nTickEnable != bool)
			  {
				  if(ms_nTicker == null)
				  {
					  ms_nTicker = JTick.createTicker(onTickEvent);
				  }
				  else
				  {
					  ms_nTicker.start();
				  }
			  }
			  return ;
		  }
	  }
	  public function get TickEnable():Boolean
	  {
		  return ms_nTickEnable;
	  }
	  
	  protected function onTickEvent(delay:Number):void
	  {
		  
	  }     
      
      protected function init() : void 
	  {
		 this.addChildren();
		 this.invalidate();
		 this.addEventListener(Event.ADDED_TO_STAGE,this.onaddto);
		 this.addEventListener(Event.REMOVED_FROM_STAGE,onRelease);
      }
      
      protected function addChildren() : void 
	  {
		
      }
      
	  protected function onRelease(event:Event=null):void
	  {
		  if(ms_nTickEnable && ms_nTicker)
		  {
			  ms_nTicker.stop();
		  }
	  }
	  
      public function invalidate(event:Event=null) : void 
	  {
         addEventListener(Event.ENTER_FRAME,this.onInvalidate);
      }
      
      public function invalidateNow() : void 
	  {
         this.onInvalidate(null);
      }
      
      public function move(cx:Number, cy:Number) : void 
	  {
         this.x = Math.round(cx);
         this.y = Math.round(cy);
      }
      
      public function setSize(cw:Number, ch:Number) : void 
	  {
         this.width = cw;
         this.height = ch;
      }
      
      public function draw() : void 
	  {
         this.drawed = true;
         if(stage)
         {
            this.graphics.clear();
            dispatchEvent(new Event(JSprite.DRAW));
         }
         else
         {
            this.addEventListener(Event.ADDED_TO_STAGE,this.onaddto);
         }
      }
      
      private function onaddto(event:Event) : void 
	  {
         this.removeEventListener(Event.ADDED_TO_STAGE,this.onaddto);
         this.graphics.clear();
		 this.onAddStageEvent();
         dispatchEvent(new Event(JSprite.DRAW));
		 
      }
      
	  protected function onAddStageEvent():void
	  {
		  
	  }
	  
      protected function onInvalidate(event:Event) : void 
	  {
         removeEventListener(Event.ENTER_FRAME,this.onInvalidate);
         this.draw();
      }
      
      public function set skin(skinName:String) : void 
	  {
         if(this._skin != skinName)
         {
            if(Skin.getSkin(this._skin))
            {
               Skin.getSkin(this._skin).unhook(this);
            }
            this._skin = skinName;
            if(Skin.getSkin(this._skin))
            {
               Skin.getSkin(this._skin).hook(this);
            }
            this.invalidate();
         }
	  }
      
      public function get skin() : String 
	  {
         return this._skin;
      }
      
      protected var _data:Object;
      
      public function get data() : Object 
	  {
         return this._data;
      }
      
      public function set data(param1:Object) : void 
	  {
         this._data = param1;
         this.invalidate();
      }
      
      
      public function get enabled() : Boolean 
	  {
         return this._enabled;
      }
      
      public function set enabled(bool:Boolean) : void 
	  {
         this._enabled = bool;
		 this.mouseEnabled = bool;
      }
      
      override public function set width(cw:Number) : void 
	  {
         if(this._width != cw)
         {
            this._width = cw;
            this.invalidate();
         }
      }
      override public function get width() : Number 
	  {
         return this._width;
      }
      
      override public function set height(ch:Number) : void 
	  {
         if(this._height != ch)
         {
            this._height = ch;
            this.invalidate();
         }
      }
      override public function get height() : Number 
	  {
         return this._height;
      }
      
      override public function set x(cx:Number) : void 
	  {
         super.x = Math.round(cx);
      }
      
      override public function set y(cy:Number) : void 
	  {
         super.y = Math.round(cy);
      }
      
      public function get state() : int 
	  {
         return this._state;
      }
      
      public function set state(status:int) : void 
	  {
         if(status != this._state)
         {
            this._state = status;
            if(this.hasState)
            {
               this.invalidate();
            }
         }
      }
      
      public function get isShowing() : Boolean 
	  {
         return !(null == stage);
      }
      
      public function hide(event:Event=null) : void 
	  {
         if(parent)
         {
            parent.removeChild(this);
         }
      }
      
      public function show(parent:DisplayObjectContainer) : void 
	  {
         if(parent)
         {
			 parent.addChild(this);
         }
      }
	  
	  /**----------------------------------以下是静态的全局方法------------------------------*/
	  /**
	   * 移除某个显示对象的所有子对象
	   */
	  static public function removeAllChildren(dis:DisplayObjectContainer):void
	  {
		  while(dis && dis.numChildren)
		  {
			  dis.removeChildAt(0);
		  }
	  }
	  
	  /**
	   * 试着从显示对象中移除某个子对象
	   */
	  static public function removeFromDisplay(dis:DisplayObjectContainer,child:DisplayObject):Boolean
	  {
		  try
		  {
			  if(dis.contains(child))
			  {
				  dis.removeChild(child);
				  return true;
			  }
			  return false;
		  }
		  catch(err:Error)
		  {
			  
		  }
		  return false;
	  }
	  
	  /**
	   * 禁用或者启用某显示对象及其子对象的鼠标感应
	   */
	  static public function setMouseEnble(dis:DisplayObjectContainer,bool:Boolean):void
	  {
		  if(dis)
		  {
			  dis.mouseEnabled = bool;
			  dis.mouseChildren = bool;
		  }
	  }
   }
}
